package UI;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import junit.framework.TestCase;
import data_management.*;

public class ScrollTableTest extends TestCase {

	/**
	 * This test provides a valid (ie: not null) input for the initializer. 
	 * The columns in the ScrollTable should match the input in order and value.
	 */
	public void testValidInitialize() {
		
		// Create an arbitrary input array
		Vector<String> input_columns = new Vector<String>();
		input_columns.add("Name");
		input_columns.add("Type");
		input_columns.add("Price");
		input_columns.add("Origin");
		input_columns.add("Fanciness");
		input_columns.add("Other Column Name");
		
		// Create a scroll table using that as an argument
		ScrollTable testTable = new ScrollTable(input_columns);
		
		// Prove that all the columns in the input match the final table
		for(int i = 0; i < input_columns.size(); i++) {
			assertTrue(input_columns.get(i).equals(testTable.getModel().getColumnName(i)));
		}
	}

	/**
	 * This test provides an invalid (ie: null) input for the initializer. 
	 * It should create a table with no columns.
	 */
	public void testNullInitialize() {
		
		ScrollTable testTable = new ScrollTable(null);
		
		// Prove that the output table has no columnns
		assertTrue(testTable.getModel().getColumnCount() == 0);
	}
	
	/**
	 * This test updates an empty scroll table with the contents of a database.
	 */
	public void testUpdateScrollTable() {
		
		// Grab a database reference
		InvDBManager db = InvDBManager.getInstance();
		db.initializeDatabase();

		// Get all the contents of the database
		ResultSet set = db.searchDatabase("%", InvDBManager.NAME);
		
		// make a new table using the database columns
		ScrollTable table = new ScrollTable(db.getColumns("inventory"));
				
		// update the table
		table.updateTable(set);
		
		// Verify that the table matches the result set
		try {
			
			// get number of rows
			int rowCount = 0;
			set = db.searchDatabase("%", InvDBManager.NAME);
			
			// checks the tostring of each value and compares them
			while(set.next()) {
				for(int i = 0; i < table.getModel().getColumnCount(); i++) {
					assertTrue(table.getModel().getValueAt(rowCount, i).toString().equals(set.getString(i+1)));
				}
				
				rowCount++;
			}
			
			assertTrue(table.getModel().getRowCount() == rowCount);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.cleanUp();
	}
	
	/**
	 * This test removes some selected rows from a table.
	 */
	public void testRemove() {
		
		// Grab a database reference
		InvDBManager db = InvDBManager.getInstance();
		db.initializeDatabase();
		
		// Make two matching tables
		ScrollTable table1 = new ScrollTable(db.getColumns("inventory"));
		
		// Select all rows, and then remove them
		table1.getTable().selectAll();
		table1.removeSelected();
		
		assertTrue(table1.getModel().getRowCount() == 0);
		
		db.cleanUp();
	}
	
	/**
	 * This test copies data from one table to another. Only the first selected is transferred
	 */
	public void testCopy() {
		// Grab a database reference
		InvDBManager db = InvDBManager.getInstance();
		db.initializeDatabase();

		// Make empty tables
		Vector<String> columns = db.getColumns("inventory");
		ScrollTable table1 = new ScrollTable(columns);
		ScrollTable table2 = new ScrollTable(columns);
		
		// Fill one table
		table1.updateTable(db.searchDatabase("%", InvDBManager.NAME));
		
		// Copy table1 to table2
		table2.copyFrom(table1);
		
		// Verify that the tables match
		for(int i = 0; i < table1.getModel().getColumnCount(); i++) {
			for(int j = 0; j < table2.getModel().getRowCount(); j++) {
				assertTrue(table1.getModel().getColumnCount() == table2.getModel().getColumnCount());
			}
		}
		
		db.cleanUp();
	}

}
